package com.ftg.learn.framework.mybatis.javabase.mapper.sqlbuilder;

import com.ftg.learn.framework.mybatis.mybatisinterface.entity.StudentEntity;
import org.apache.ibatis.jdbc.SQL;

import java.util.Objects;

/**
 * 学生的构建器
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/9/5 11:11
 */

public class StudentSqlBuilder {
    /**
     * 纯java实现
     * @param stu_sex  性别
     * @param stu_name 姓名
     * @return
     */
    public String studentSelect(String stu_sex, String stu_name) {
        //纯java
        String sql = "select * from student where 1 ";
        if(Objects.nonNull(stu_sex) && !stu_sex.isEmpty()){
            sql += " and stu_sex = #{stu_sex}";
        }
        if(Objects.nonNull(stu_name) && !stu_name.isEmpty()){
            sql += " and stu_name = #{stu_name}";
        }
        return sql;
    }

    public String studentSelect1(String stu_sex, String stu_name) {
        SQL sql = new SQL();
        sql.SELECT("stu_name,stu_sex,classno")
                .FROM("student");
        if(Objects.nonNull(stu_sex) && !stu_sex.isEmpty()){
            sql.WHERE("stu_sex = #{stu_sex}");
        }
        if(Objects.nonNull(stu_name) && !stu_name.isEmpty()){
            sql.AND().WHERE("stu_name = #{stu_name}");
        }
        return sql.toString();
    }

    public String insertSave(StudentEntity se) {
        SQL sql = new SQL();
        sql.INSERT_INTO("student").INTO_COLUMNS("stu_name","stu_sex").VALUES(se.getStu_name(),se.getStu_sex());
        return sql.toString();
    }

}
